package com.rc.evcharger.common.utils;

import java.util.Calendar;

/**
 * Create by Yang on 2021/9/7
 */
public class JnDataUtil {


    /**
     * 京能校验规则生成
     */
    public static String checkNumSumAll(String[] strs) {

        // 二进制求和
        String bin = NumberUtil.binarySumByAll(strs);

        // 二进制转十进制
        int ten = NumberUtil.binToTen(bin);

        // 对256求模
        //int yu = ten % 256;

        // 十进制转十六进制
        String hex = Integer.toHexString(ten).toUpperCase();

        //补0
        StringBuffer stringBuffer = new StringBuffer(hex);
        for (int i = stringBuffer.length(); i < 8; i++) {
            stringBuffer.insert(0, 0);
        }

        return stringBuffer.substring(6) + " "
                + stringBuffer.substring(4, 6) + " "
                + stringBuffer.substring(2, 4) + " "
                + stringBuffer.substring(0, 2);
    }


    /**
     * 根据倍率将数据转成实际数据
     *
     * @param value
     * @param rate
     * @param isHex 是否转为10进制
     * @return
     */
    public static int getIntBDValue(String value, boolean isHex) {
        value = DataUtil.turnValue(value);
        int val = 0;
        if (isHex) {
            val = Integer.parseInt(value, 16);
        }
        return val;
    }

    /**
     * 获取当前时间的BCD码帧格式
     */
    public static String getBCDTimeFreame() {

        StringBuffer stringBuffer = new StringBuffer();

        Calendar instance = Calendar.getInstance();

        //年 高低位反转
        String year = instance.get(Calendar.YEAR) + "";
        stringBuffer.append(year.substring(2)).append(DataUtil.SPLIT);
        stringBuffer.append(year.substring(0, 2)).append(DataUtil.SPLIT);

        //月
        String month = (instance.get(Calendar.MONTH) + 1) + "";
        if (month.length() == 1) {
            month = "0" + month;
        }
        stringBuffer.append(month).append(DataUtil.SPLIT);

        //日
        String day = instance.get(Calendar.DAY_OF_MONTH) + "";
        if (day.length() == 1) {
            day = "0" + day;
        }
        stringBuffer.append(day).append(DataUtil.SPLIT);


        //时
        String hour = instance.get(Calendar.HOUR_OF_DAY) + "";
        if (hour.length() == 1) {
            hour = "0" + hour;
        }
        stringBuffer.append(hour).append(DataUtil.SPLIT);

        //分
        String min = instance.get(Calendar.MINUTE) + "";
        if (min.length() == 1) {
            min = "0" + min;
        }
        stringBuffer.append(min).append(DataUtil.SPLIT);

        //秒
        String sed = instance.get(Calendar.SECOND) + "";
        if (sed.length() == 1) {
            sed = "0" + sed;
        }
        stringBuffer.append(sed);

        return stringBuffer.toString();
    }


    /**
     * 构造订单号帧,20字节,后5字节补00
     *
     * @param orderNum
     * @param length
     * @return
     */
    public static String getJnOrderNumFrame(String orderNum, int length) {
        char[] c = orderNum.toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < c.length; i = i + 2) {
            sb.append(c[i]).append(c[i + 1]).append(DataUtil.SPLIT);
        }

        for (int i = 0; i < length; i++) {
            sb.append("00").append(DataUtil.SPLIT);
        }

        return sb.toString().trim();
    }


    public static String getTerminalIdFrame(String pileNo) {
        Integer integer = Integer.valueOf(pileNo.replace(" ", ""));
        String hex = Integer.toHexString(integer);
        int length = 8 - hex.length();
        for (int i = 0; i < length; i++) {
            hex = "0" + hex;
        }
        hex = hex.toUpperCase();
        //StringBuffer stringBuffer = new StringBuffer(hex);
        //hex = stringBuffer.reverse().toString().toUpperCase();

        //return hex.substring(0, 2) + " "
        //        + hex.substring(2, 4) + " "
        //        + hex.substring(4, 6) + " "
        //        + hex.substring(6, 8);
        return hex.substring(6, 8) + " "
                + hex.substring(4, 6) + " "
                + hex.substring(2, 4) + " "
                + hex.substring(0, 2);
    }


    public static void main(String[] args) {
        JnDataUtil jnDataUtil = new JnDataUtil();
        String jnOrderNumFrame = JnDataUtil.getTerminalIdFrame("81580521");
        System.out.println(jnOrderNumFrame);
    }
}
